﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XZL.GM.Entity;
using XZL.GM.Bll;
using XZL.GM.Dal;

public partial class Admin_User_UserEdit : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDDL();
            BindUserInfo();
        }
    }


     /// <summary>
    /// 绑定下拉列表
    /// </summary>
    private void BindDDL()
    {
        ddlStatus.DataSource = UserInfoBiz.GetUserStatusList();
        ddlStatus.DataTextField = "StatusName";
        ddlStatus.DataValueField = "StatusID";
        ddlStatus.DataBind();

        ddlUserType.DataSource = UserInfoBiz.GetUserTypeList();
        ddlUserType.DataTextField = "UserTypeName";
        ddlUserType.DataValueField = "UserTypeID";
        ddlUserType.DataBind();
    }


    /// <summary>
    /// 初始化数据
    /// </summary>
    private void BindUserInfo()
    {
        string userID = Request.QueryString["UserID"];

        if (userID != null)
        {
             
            Proc_UserInfo_SelectUserInfoResult user = UserInfoBiz.GetUserInfo(userID);
            if (user != null)
            {
              
              
                txtNickName.Text=user.NickName;
                txtMobilePhone.Text = user.MobilePhone;
                txtWeiXinName.Text = user.WeiXinName;
             
                ddlUserType.SelectedValue = user.UserType.ToString();
                ddlStatus.SelectedValue = user.Status.ToString();

                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                if (user.HeadPortrait == "" || user.HeadPortrait == null)
                {

                }
                else
                {
                    imgSignature.ImageUrl =imgPath + user.HeadPortrait;
                    lblFileName.Text = user.HeadPortrait;
                }

            }

        }
    }



    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string userID = Request.QueryString["UserID"];

        if (userID != null)
        {
            
            //验证姓名
            if (txtNickName.Text.Trim() == "")
            {
                lblWarn.Text = "请输入您的昵称！";
                return;
            }

            string headPortrait = lblFileName.Text;
            


            bool b = UserInfoBiz.UpdateUserInfo(userID, txtNickName.Text, txtMobilePhone.Text, txtWeiXinName.Text, headPortrait, Convert.ToInt32(ddlUserType.SelectedValue), Convert.ToInt32(ddlStatus.SelectedValue));
            if (b)
            {
                Response.Redirect("UserList.aspx");
            }

        }
    }


    /// <summary>
    /// 上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        if (txtSelectFile.HasFile)
        {
            HttpPostedFile postedFile = txtSelectFile.PostedFile;
            string fileName = System.IO.Path.GetFileName(postedFile.FileName);
            if (fileName != "")
            {
                string fileExtension = System.IO.Path.GetExtension(fileName);
                string newName = Guid.NewGuid().ToString();
                string imgPath = System.Configuration.ConfigurationManager.AppSettings["ImgPath"];
                string path = System.Web.HttpContext.Current.Request.MapPath(imgPath) + newName + fileExtension;
                lblFileName.Text = newName + fileExtension;
                ///'可根据扩展名字的不同保存到不同的文件夹
                ///注意：可能要修改你的文件夹的匿名写入权限。
                postedFile.SaveAs(path);
                lblUploadInfo.Text = "上传成功";
                imgSignature.ImageUrl = imgPath + newName + fileExtension;
            }
        }
    }



    /// <summary>
    /// 返回
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserList.aspx");
    }

}